<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
        height: 34px;
        line-height: 34px;
        padding: 0 8px;
    }
</style>

<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo" lay-filter="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add" data-title="添加"><i class="fa fa-plus"></i> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete" data-title="删除"><i
                        class="fa fa-trash-o"></i> 删除
                </button>
            </div>
        </script>

        <div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>

    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;
        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            toolbar: '#toolbarDemo',
            elem: '#munu-table',
            url: '{:url("menu/index")}',
            page: false,
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'title', minWidth: 200, title: '菜单名称'},
                {
                    field: 'icon', title: '图标', templet: function (d) {
                        if (d.icon != '') {
                            return '<i class="fa ' + d.icon + '"></i>';
                        }
                    }
                },
                {field: 'url', title: '菜单链接'},
                {field: 'weight', width: 80, align: 'center', title: '排序'},
                {
                    field: 'is_index', width: 80, align: 'center', templet: function (d) {
                        if (d.is_index == 1) {
                            return '<span class="layui-badge layui-bg-blue">首页</span>';
                        } else if (d.pid == 0) {
                            return '<span class="layui-badge layui-bg-gray">模块</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });
        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });
        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        table.on('toolbar(munu-table)', function (obj) {
            var event = obj.event;
            if (event === 'add') {  // 监听添加操作
                openDetail('新增','{:url("menu/add")}');
            } else if (event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('munu-table')
                    , data = checkStatus.data;
                var ids = [];
                for (var i=0;i<data.length;i++){
                    ids.push(data[i]['id']);
                }
                delItem('{:url("menu/del")}',ids);
            }
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') {
                delItem('{:url("menu/del")}',data.id);
            } else if (layEvent === 'edit') {
                var url = '{:url("menu/edit",["id"=>"ID"])}';
                url = url.replace("ID",data.id);
                openDetail('修改',url);
            }
        });
    });
</script>

